<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'image';
$types=array(
	'image'=>'图片',
	'audio'=>'音频',
	'video'=>'视频',
);
if($_RQ['op']=='image'){
	//搜索
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']='image';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['group_id']) && $_RQ['group_id']!=-10){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}
	if(!isset($return['params']['group_id'])){
		$return['params']['group_id']=-10;
	}
	$fields=array('title','attachment','id');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 21;
	$total='';
	$list=pdo_getslice('core_attachment',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$row){
		if(!$row['title']){
			$row['title']='未设置标题';
		}
	}
	$return['groups']=pdo_getall('core_attachment_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	if($return['groups']){
		foreach($return['groups'] as &$group){
			$group['num']=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'type'=>'image','group_id'=>$group['id']));
		}
	}else{
		$return['groups']=array();
	}
	$no_group=array('id'=>0,'title'=>'未分组');
	$no_group['num']=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'type'=>'image','group_id'=>0));
	array_unshift($return['groups'],$no_group);
	$manage=array('id'=>-1,'title'=>'系统图片');
	$manage['num']=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'type'=>'image','group_id'=>-1));
	array_unshift($return['groups'],$manage);
	$custom=array('id'=>-2,'title'=>'用户图片');
	$custom['num']=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'type'=>'image','group_id'=>-2));
	array_unshift($return['groups'],$custom);
	$all=array('id'=>-10,'title'=>'全部图片');
	$all['num']=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'type'=>'image'));
	array_unshift($return['groups'],$all);
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['total']=$total;
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='audio'){
	//搜索
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']='audio';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('title','attachment','id','createtime');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 21;
	$total='';
	$list=pdo_getslice('core_attachment',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$row){
		$row['createtime']=date('Y-m-d H:i',$row['createtime']);
		if(!$row['title']){
			$row['title']='未设置标题';
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='video'){
	//搜索
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']='video';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('title','attachment','id');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 21;
	$total='';
	$list=pdo_getslice('core_attachment',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$row){
		if(!$row['title']){
			$row['title']='未设置标题';
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='add'){
	$return=array('item'=>array());
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'image','title'=>'返回附件列表'),
			array('op'=>'add','title'=>'批量上传附件'),
		),
		'edit'=>array(
			array('type'=>'radio','name'=>'type','title'=>'上传附件类型','options'=>'types','help'=>'上传时，系统会自动忽略文件格式不合法的文件'),
			array('type'=>'upfile','name'=>'files','title'=>'上传附件压缩包','help'=>'请上传zip格式压缩包。 <a href="\'.SITEROOT.\'core/resource/imagesdemo.zip">【下载示例压缩包】</a> 上传前可以先对图片进行分组，放到普通文件夹，系统将自动识别文件夹并将图片存放到相应附件分组下！'),
		),
	);
	$return['types']=$types;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$type=$postdata['type'];
	if(!$types[$type] || !$postdata['files']){
		exi('请先选择要上传的附件类型和附件压缩包！','error');
	}
	$setting=core_setting('upload');
	$yes=str_array($setting[$type.'_ext']);
	foreach($yes as &$ye){
		$ye='.'.$ye;
	}
	$file=cfc('files')->local($postdata['files']);
	$temp=ROOT_AT.random(30);
	$zip=new ZipArchive();
	$zip->open($file);
	$zip->extractTo($temp);
	$files=cfc('files')->file_tree($temp,array(),$yes);
	foreach($files as $row){		
		$file_array=explode("/",$row,3);	
		if(count($file_array)==2){
			$filename=$file_array[1];
			$dir='';
		}else{
			$filename=$file_array[2];
			$dir=$file_array[1];
		}
		$index=strpos($filename,'.');
		$ext=substr($filename,$index+1);
		$filename=substr($filename,0,$index);
		if($dir){
			$dir=iconv('gbk',"utf-8//IGNORE",$dir);
			$group_id=pdo_getcolumn('core_attachment_group',array('uniacid'=>$_SESSION['uniacid'],'title'=>$dir),'id');
			if(!$group_id){
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>$dir,
				);
				pdo_insert('core_attachment_group',$insert);
				$group_id=pdo_insertid();
			}
		}else{
			$group_id=0;
		}
		$filename=iconv('gbk',"utf-8//IGNORE",$filename);
		cfc('files')->source_save($temp.$row,$type,$ext,$filename,'',$group_id);
	}
	cfc('files')->rmdirs($temp);
	exi('批量增加附件成功！','','image');
}
if($_RQ['op']=='ctitle'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$id=intval($postdata['id']);
		if(!$id){
			exi('请先选择附件1！','error');
		}
		$postdata=get_postdata();
		$filename=trim($postdata['title']);
		if($filename){
			pdo_update('core_attachment',array('title'=>$filename),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('附件名称修改成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择附件2！','error');
	}
	$return=array();
	$return['item']=pdo_get('core_attachment',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改附件名称',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'附件名称'),
		),
	);
	exi($return);
}
if($_RQ['op']=='cgroup'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$id=intval($postdata['id']);
		$ids=trim($postdata['ids']);
		if(!$id && !$ids){
			exi('请先选择附件！','error');
		}
		if($ids){
			$ids=trim($ids,';');
			if($ids){
				$ids=explode(';',$ids);
			}
		}
		if(!$ids){
			$ids=array();
		}
		if($id){
			$ids[]=$id;
		}
		$group_id=intval($postdata['group_id']);
		if($group_id){
			foreach($ids as $id){
				$id=intval($id);
				if($id){
					pdo_update('core_attachment',array('group_id'=>$group_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		exi('附件分组修改成功！');
	}
	$id=intval($_RQ['id']);
	$ids=trim($_RQ['ids']);
	if(!$id && !$ids){
		exi('请先选择附件！','error');
	}
	$return=array();
	$return['item']=array('id'=>$id,'ids'=>$ids);
	$return['groups']=pdo_getall('core_attachment_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['groups'][]=array(
		'id'=>-1,
		'title'=>'系统图片',
	);
	$return['groups'][]=array(
		'id'=>-2,
		'title'=>'用户图片',
	);
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'移动附件分组',
		'edit'=>array(
			array('type'=>'select','name'=>'group_id','title'=>'所属分组','options'=>'groups'),
		),
	);
	exi($return);
}
if($_RQ['op']=='add_group'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$cs=var_export($postdata,true);
		//exi($cs,'error');
		$title=trim($postdata['title']);
		if($id){
			pdo_update('core_attachment_group',array('title'=>$title),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('分组修改成功！');
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'title'=>$title,
			);
			pdo_insert('core_attachment_group',$insert);
			exi('分组添加成功！');
		}
	}
	if($id){
		$item=pdo_get('core_attachment_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	}else{
		$item=array();
	}
	$return=array('item'=>$item);
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'分组名称'),
		),
	);
	if($id){
		$return['htmls']['desc']='编辑分组';
	}else{
		$return['htmls']['desc']='新增分组';
	}
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		cfc('files')->source_del($id);
		exi('附件删除成功！');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				cfc('files')->source_del($id);
			}
		}
	}
	exi('附件删除成功！');
}
if($_RQ['op'] == 'clear'){
	//清理附件
	$num=pdo_count('core_attachment',array('uniacid'=>$_SESSION['uniacid']));
	$pages=intval(ceil($num/1000));
	$dels=array();
	for($i=1;$i<=$pages;$i++){
		$list=pdo_getslice('core_attachment',array('uniacid'=>$_SESSION['uniacid']),array($i,1000),$total,array('id','attachment'));
		if($list){
			foreach($list as $item){
				if(!ATTACHMENT_TYPE){
					if(!is_file(ROOT_AT.$item['attachment'])){
						$dels[]=$item['id'];
					}
				}else{
					$check=get_headers(tomedia($item['attachment']),true);
					
				}
			}
		}
	}
	if($dels){
		pdo_delete('core_attachment',array('id'=>$dels));
	}
	exi('清理成功！');
}
function addfujian($src,$type){
	global $_AR;
	$dir = opendir($src);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                addfujian($src . '/' . $file);
            }
            else {
				$filen=iconv('EUC-CN',"utf-8//IGNORE",$file);
				$temp=strrchr($file,'.');
				$title=str_replace($temp,'',$filen);
				$ext=strtolower(substr($temp,1));
				$setting=core_setting('upload');
				$yes=trim($setting[$type.'_ext'],';');
				$yes=explode(';',$yes);
				if(in_array($ext,$yes)){
					cfc('files')->source_save($src . '/' . $file,$type,$ext,$title);
				}
            }
        }
    }
    closedir($dir);
}
